package com.fit.pages.wholesale;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumSelectModel;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.wholesale.WholesaleDAO;
import com.fit.entities.Wholesale;


@AdminAccess
public class EditWholesale {

	@Property
	@Persist
	private Wholesale wholesale;
	
	@Inject
	private WholesaleDAO wholesaleDAO;
	
	@InjectPage
	private ManageWholesales manage;
	
	@Inject
	private Messages messages;
	

	
//	public SelectModel getTypes() {
//		return new EnumSelectModel(VehicleType.class, messages);
//	}
	
	Object onActivate(String name) {
		manage.setError("");
		manage.setConfirmation("");
		Wholesale w = wholesaleDAO.findByName(name);
		if (w != null) {
			wholesale = w;
		}
		if (wholesale == null) {
			manage.setError("Error");
			return manage;
		}
		return null;
	}

	Object onSuccess() {
		manage.setError("");
		manage.setConfirmation("");
		try {
//			if (vehicleType !=null && !vehicle.getTypeOfVehicle().equalsIgnoreCase(vehicleType.toString())) {
//				vehicle.setTypeOfVehicle(vehicleType.toString());
//			}
			wholesaleDAO.update(wholesale);
			manage.setConfirmation("Successfully edited.");
			return manage;
		} catch (Exception e) {
			manage.setError("Error.");
			return manage;
		}

	}

}
